package com.zut.dao.impl;

import com.zut.dao.ManagerDao;
import com.zut.entity.Manager;
import com.zut.util.JdbcUtil;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 管理员持久层实现类
 */
public class ManagerDaoImpl implements ManagerDao {

    //接收JdbcTemplate对象
    private JdbcTemplate template = JdbcUtil.template;

    /**
     * 根据账号和密码查询管理员
     * @param username
     * @param password
     * @return
     */
    @Override
    public Manager findManagerByUsernameAndPassword(String username, String password) {
        String sql = "select * from manager where username=? and password=?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<>(Manager.class), username, password);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

    }

    /**
     * 修改密码
     * @param username
     * @param newPwd
     */
    @Override
    public void updatePassword(String username, String newPwd) {
        String sql = "update manager set password=? where username=?";
        template.update(sql,newPwd,username);

    }
}
